/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bytedance.scene.ui.view.ext;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.Component;

/**
 * 沉浸式状态栏
 *
 * @since 2021-06-16
 */
public class ReboundHelper implements AnimatorValue.ValueUpdateListener, Animator.StateChangedListener {
    private static final int DEFAULTDURATION = 200;

    private ReboundOverListener mListener;

    private AnimatorValue animatorValue;

    private Component mComponent;

    private float mFromX;
    private float mFromY;
    private float mToX;
    private float mToY;

    /**
     * ReboundHelper
     *
     * @param listener listener
     */
    public ReboundHelper(ReboundOverListener listener) {
        mListener = listener;
        animatorValue = new AnimatorValue();
        animatorValue.setDuration(DEFAULTDURATION);
        animatorValue.setCurveType(Animator.CurveType.ACCELERATE);
        animatorValue.setValueUpdateListener(this::onUpdate);
        animatorValue.setStateChangedListener(this);
    }

    /**
     * reboundX
     *
     * @param component component
     * @param toX toX
     */
    public void reboundX(Component component, float toX) {
        rebound(component, toX, component.getTranslationY());
    }

    /**
     * reboundY
     *
     * @param component component
     * @param toY toY
     */
    public void reboundY(Component component, float toY) {
        rebound(component, component.getTranslationX(), toY);
    }

    /**
     * rebound
     *
     * @param component component
     * @param toX toX
     * @param toY toY
     */
    public void rebound(Component component, float toX, float toY) {
        if (animatorValue.isRunning()) {
            return;
        }
        mComponent = component;
        mFromX = component.getTranslationX();
        mFromY = component.getTranslationY();
        mToX = toX;
        mToY = toY;
        animatorValue.start();
    }

    /**
     * duration
     *
     * @param rate rate
     * @return ReboundHelper
     */
    public ReboundHelper duration(float rate) {
        animatorValue.setDuration((long) (rate * DEFAULTDURATION));
        return this;
    }

    /**
     * stop
     */
    public void stop() {
        if (animatorValue.isRunning()) {
            animatorValue.stop();
        }
    }

    @Override
    public void onUpdate(AnimatorValue animatorValue, float value) {
        float offsetX = mFromX + (mToX - mFromX) * value;
        float offsetY = mFromY + (mToY - mFromY) * value;
        mListener.update(offsetX, offsetY);
    }

    @Override
    public void onStart(Animator animator) {
    }

    @Override
    public void onStop(Animator animator) {
    }

    @Override
    public void onCancel(Animator animator) {
    }

    @Override
    public void onEnd(Animator animator) {
        mComponent.setTranslation(mToX, mToY);
        animatorValue.setDuration(DEFAULTDURATION);
        mListener.over();
    }

    @Override
    public void onPause(Animator animator) {
    }

    @Override
    public void onResume(Animator animator) {
    }

    /**
     * ReboundOverListener
     *
     * @since 2021-06-16
     */
    public interface ReboundOverListener {
        /**
         * update
         *
         * @param offsetX offsetX
         * @param offsetY offsetY
         */
        void update(float offsetX, float offsetY);

        /**
         * over
         */
        void over();
    }
}
